//
//  信息提示_VC.swift
//  UITest_Swift
//
//  Created by 郭斌勇 on 16/8/20.
//  Copyright © 2016年 郭斌勇. All rights reserved.
//

import UIKit

class 信息提示_VC: UIViewController {
    
    var 显示时长:NSTimeInterval = 1
    
    var 标题:String?{
        didSet{
            if let 解封标题 = 标题 {
                标题lab.text = 解封标题
                标题lab.hidden = false
            }else{
                标题lab.text = nil
                标题lab.hidden = true
            }
        }
        
    }
    
    var 信息:String?{
        didSet{
            if let 解封信息 = 信息 {
                信息lab.text = 解封信息
                信息lab.hidden = false
            }else{
                信息lab.text = nil
                信息lab.hidden = true
            }
        }
        
    }
    
    
    var 宽度比例:Double?{
        
        didSet{
            if let 解封宽度比例 = 宽度比例 {
                容器viewWidth.setValue(NSNumber(float: Float(解封宽度比例)), forKey: "multiplier")
            }else{
                容器viewWidth.setValue(NSNumber(float: Float(2.0/3)), forKey: "multiplier")
            }
        }
        
        
    }
    
    
    var 中心垂直偏移:Double?{
        didSet{
            if let 解封中心垂直偏移 = 中心垂直偏移 {
                容器viewCenterYCenterY.constant = CGFloat(解封中心垂直偏移)
            }else{
                容器viewCenterYCenterY.constant = CGFloat(0)
            }
        }
        
    }
    
    
    var 背景色:UIColor?{
        
        didSet{
            if let 解封背景色 = 背景色 {
                容器view.backgroundColor = 解封背景色
            }else{
                 容器view.backgroundColor = UIColor.clearColor()
            }
            
        }
        
    }
    

    @IBOutlet weak var 容器view: UIView!
    @IBOutlet weak var 标题lab: UILabel!
    @IBOutlet weak var 信息lab: UILabel!
    @IBOutlet weak var 容器viewWidth: NSLayoutConstraint!
    @IBOutlet weak var 容器viewCenterYCenterY: NSLayoutConstraint!
    


    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    

   
    init(){
        super.init(nibName:nil, bundle: nil)
        
        let nib = UINib(nibName:"信息提示_VC", bundle: nil)
        nib.instantiateWithOwner(self, options: nil)
        
        modalPresentationStyle = .OverCurrentContext
        modalTransitionStyle = .CrossDissolve
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func viewDidAppear(animated: Bool) {
        self.performSelector("消失", withObject: nil, afterDelay: 显示时长)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func 点击背景(sender: UITapGestureRecognizer) {
        消失()
    }

    func 消失(){
        self.dismissViewControllerAnimated(true, completion: {self.恢复到默状态()})
    }
    
    func 显示(){
        
        if isBeingPresented() {
            return
        }
        

        顶层viewController()?.presentViewController(self, animated: true, completion: nil)
        
        
    }
    
    
    func 顶层viewController()->UIViewController?{
        
        var 上层vc = UIApplication.sharedApplication().keyWindow?.rootViewController
        
        while ((上层vc?.presentedViewController) != nil) {
            上层vc = 上层vc?.presentedViewController
        }
        
        return 上层vc
    }
    
    
    func 恢复到默状态(){
        标题 = nil
        信息 = nil
    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
